package com.weiwei.diancan.adapter;

import android.content.Context;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.weiwei.diancan.R;
import com.weiwei.diancan.bean.LoginBean;
import com.weiwei.diancan.bean.MenuBean;
import com.weiwei.diancan.bean.SelectOrder;
import com.weiwei.diancan.utils.LoggerUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import weiwei.com.weiweilibrary.adapter.BaseQuickAdapter;
import weiwei.com.weiweilibrary.holder.BaseViewHolder;
import weiwei.com.weiweilibrary.views.ToastUtil;

/**
 * Created by BMW on 2016/11/23.
 */
public class HasOrderAdapter extends BaseQuickAdapter<List> {
    private Context context;
    private OnItemClickListener listener;
    private List<SelectOrder> isSelectOrder;

    private TextView tv_caipin;
    private TextView tv_shuliang;
    private TextView tv_jiage;

    public void setTv_caipin(TextView tv_caipin) {
        this.tv_caipin = tv_caipin;
    }

    public void setTv_shuliang(TextView tv_shuliang) {
        this.tv_shuliang = tv_shuliang;
    }

    public void setTv_jiage(TextView tv_jiage) {
        this.tv_jiage = tv_jiage;
    }

    public HasOrderAdapter(List<List> data, Context context, List<SelectOrder> isSelectOrder) {
        super(R.layout.item_has_order, data);
        this.context = context;
        this.isSelectOrder = isSelectOrder;
    }

    public void setListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public List<SelectOrder> getIsSelectOrder() {
        return isSelectOrder;
    }

    @Override
    protected void convert(BaseViewHolder helper, List item, int position) {

        RecyclerView recycler = (RecyclerView) helper.getView(R.id.recycler);
        LinearLayout ll_item = (LinearLayout) helper.getView(R.id.ll_item);
        recycler.setLayoutManager(new LinearLayoutManager(context));
        recycler.setItemAnimator(new DefaultItemAnimator());

        ll_item.setOnClickListener(v->{
            if(listener!=null) {
                listener.onItemClick(ll_item, position);
            }
        });
        if(position == 0){
            ll_item.setBackgroundResource(R.color.white);
            List<SelectOrder> selectOrders = (List<SelectOrder>) item;
            NoMenuAdapter noMenuAdapter = new NoMenuAdapter(selectOrders);
            recycler.setAdapter(noMenuAdapter);
            noMenuAdapter.setOnItemClick(new OnItemClick() {
                @Override
                public void onAdd(SelectOrder menuBean, int pos, TextView textView) {
                    double shuliang = Double.parseDouble(tv_shuliang.getText().toString());
                    double jiage = Double.parseDouble(tv_jiage.getText().toString().replace("¥",""));
                    if(isSelectOrder!=null){
                        SelectOrder selectOrder = isSelectOrder.get(pos);
                        double log_count = Double.parseDouble(selectOrder.getLog_count());
                        double log_price = Double.parseDouble(selectOrder.getLog_price());
                        selectOrder.setLog_count((log_count+1)+"");
                        tv_shuliang.setText((shuliang+1)+"");
                        tv_jiage.setText((log_price+jiage)+"");
                    }
                }

                @Override
                public void onAjj(SelectOrder menuBean, int pos, TextView textView,boolean isCai) {
                    double shuliang = Double.parseDouble(tv_shuliang.getText().toString());
                    double jiage = Double.parseDouble(tv_jiage.getText().toString().replace("¥",""));
                    SelectOrder selectOrder = isSelectOrder.get(pos);
                    double log_count = Double.parseDouble(selectOrder.getLog_count());
                    double log_price = Double.parseDouble(selectOrder.getLog_price());
                    selectOrder.setLog_count((log_count-1)+"");

                    tv_shuliang.setText((shuliang-1)+"");
                    tv_jiage.setText((jiage-log_price)+"");

                    if(isCai) {
                        Iterator<SelectOrder> iterator = isSelectOrder.iterator();
                        while (iterator.hasNext()) {
                            SelectOrder next = iterator.next();
                            if (next.getData_id().equals(menuBean.getData_id())) {
                                iterator.remove();
                            }
                        }
                        notifyDataSetChanged();
                    }
//                    for(SelectOrder selectOrder1:isSelectOrder) {
//                        if(selectOrder1.getData_id().equals(menuBean.getData_id())){
//                            isSelectOrder.remove(selectOrder1);
//                            notifyDataSetChanged();
//                        }
//                    }
                }
            });
            helper.setVisible(R.id.ll_title,false);
        }else{
            ll_item.setBackgroundResource(R.color.e9e9e9);
            List<LoginBean.OrderListBean.LogListBean> orders = (List<LoginBean.OrderListBean.LogListBean>) item;
            recycler.setAdapter(new MenuAdapter(orders));
            helper.setVisible(R.id.ll_title,true);
        }

    }

    public interface OnItemClickListener{
        void onItemClick(View v, int pos);
    }

    class NoMenuAdapter extends BaseQuickAdapter<SelectOrder>{

        private OnItemClick onItemClick;

        public void setOnItemClick(OnItemClick onItemClick) {
            this.onItemClick = onItemClick;
        }

        public NoMenuAdapter(List<SelectOrder> data) {
            super(R.layout.item_open_detail, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, SelectOrder item, int position) {

           TextView textView =  helper.getView(R.id.tv_num);

            helper.setOnClickListener(R.id.tv_add, v -> {
                int i = Integer.parseInt(textView.getText().toString());
                i++;
                textView.setText(i+"");
                if(onItemClick!=null){
                    onItemClick.onAdd(item,position,textView);

                }
            });
            helper.setOnClickListener(R.id.tv_jj, v -> {
                int i = Integer.parseInt(textView.getText().toString());

                if(i == 0){
                    ToastUtil.showToast(context,"不能再减啦！");
                }else {
                    boolean isCai = false;
                    if(i == 1){
                        int cai = Integer.parseInt(tv_caipin.getText().toString());
                        tv_caipin.setText((cai-1)+"");
                        isCai = true;
                    }else{
                        isCai = false;
                    }
                    i--;
                    textView.setText(i+"");
                    if (onItemClick != null) {
                        onItemClick.onAjj(item, position, textView,isCai);
                    }
                }
            });
            helper.setVisible(R.id.tv_fenlei,false);

            helper.setText(R.id.tv_name,item.getLog_name());
            helper.setText(R.id.tv_num,item.getLog_count());
            helper.setText(R.id.tv_price,item.getLog_price()+"/"+item.getLog_unit());
            if(item.getLog_type().equals("1")){
                helper.setVisible(R.id.iv_tao,true);
            }else{
                helper.setVisible(R.id.iv_tao, false);
            }

        }
    }

    class MenuAdapter extends BaseQuickAdapter<LoginBean.OrderListBean.LogListBean>{

        public MenuAdapter( List<LoginBean.OrderListBean.LogListBean> data) {
            super(R.layout.item_order_swip1, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, LoginBean.OrderListBean.LogListBean item, int position) {
            helper.setText(R.id.tv_price,item.getLog_price()+"/"+item.getLog_unit());
             if(item.getLog_type().equals("1")){
                helper.setVisible(R.id.iv_tao,true);
            }else{
                helper.setVisible(R.id.iv_tao,false);
            }
            helper.setText(R.id.tv_name,item.getLog_name());

            helper.setText(R.id.tv_num,item.getLog_count());
        }
    }

    public interface OnItemClick{
        void onAdd(SelectOrder menuBean, int pos, TextView textView);

        void onAjj(SelectOrder menuBean,int pos,TextView textView,boolean isCai);

    }
}
